<%@ page language="java" import="java.util.*,com.beans.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> 
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">

<title></title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<link rel="stylesheet" type="text/css" href="css/maintable.css"></link>
</head>
	<script src="js/jquery-1.8.0.js"></script> 
	<script type="text/javascript">
		/*$(function(){
			var menuIdStr="${menuIdStr}";
			var menuIdArray=menuIdStr.split(",");
			
			Array.prototype.contains=function(e){
	 			for(var i=0;i<this.length;i++){
	 				if(this[i]==e){
	 					return true;
	 				}
	 			}
	 			
	 			return false;
	 		}

			$("input[name=menuIds]").each(function(){

	 			if(menuIdArray.contains(this.value)){
	 				this.checked=true;
	 			}	
	 		});
		});	*/
	</script>

<body>
	角色：${roleInfo.roleName}
	<form action="RoleServlet.do?flog=updateRoleMenu" method="post">
		<input type="hidden" name="roleId" value="${roleInfo.id }" />
		<table border="1" class="main_table" >
			<tr>
				<th class="firsttd">一级</th><th>二级</th>
			</tr>
			<c:forEach var="m" items="${menuList}">
				<tr>
					<td>
						<input type="checkbox" name="menuIds" id="${m.id }" value="${m.id }" onclick="checkSubItem(this)"/>${m.menuName}
					</td>
					<td>
						<c:forEach var="m_sub" items="${m.subMenuList }">
							<input type="checkbox" name="menuIds" id="${m_sub.id }" value="${m_sub.id }" class="${m.id }" onclick="checkParent(${m.id})"/>${m_sub.menuName}
						</c:forEach>
					</td>
				</tr>
			</c:forEach>
		</table>
		<input type="submit" value="提交" onclick="return confirm('确定提交吗')"  />
	</form>
	${msg }
	
	<script>
   		var menuIdStr="${menuIdStr}"; 
   		
   		var menuArray=menuIdStr.split(",");  //把权限id字符串转成数组
   		
   		//给数组动态添加一个contains方法
   		Array.prototype.contains=function(e){
   			for(var i=0;i<this.length;i++){
   				if(this[i]==e){
   					return true;
   				}
   			}
   			
   			return false;
   		};
   		
   		//取到所有的复选框,进行循环
   		$("input[type=checkbox]").each(function(){
   			if(menuArray.contains(this.value)){
   				this.checked=true;
   			}
   		});
   		
   		//勾选父级复选框,让子项全选中或取消,注意,子项中加了一个class属性 ,值为父级checkbox的 id,用这样的方式建立起来的关联
   		function checkSubItem(txtMenu){
   			$("."+txtMenu.id).attr("checked",txtMenu.checked);
   		}
   		
   		//勾选子级复选框后,处理父级复选框的状态 ,传过来的参数值是父级复选框的id
   		function checkParent(parentId){
   			$("#"+parentId).removeAttr("checked");
   			
   			$("."+parentId).each(function(){
   				if(this.checked){
   					$("#"+parentId).attr("checked","checked");
   					return;
   				}
   			});	
   		}
   		
   	</script>
   	
   	<script>
   		var msg="${msg}";
   		if(msg!=""){
   			alert(msg);
   		}
   	</script>
   	
   	<script>
   		$(function(){
   		    //处理全选
   			$("#ckall").click(function(){
   				if($("#ckall").attr("checked")=="checked"){
   					$("input[name=menuIds]").attr("checked","checked");
   				}
   				else{
   					$("input[name=menuIds]").removeAttr("checked");
   				}
   			
   			});
   			
   			//如果有不选的.也可把全选框勾掉,如果全选了,也要把全选框自动勾上
   			$("input[name=menuIds]").click(function(){
   				if($("input[name=menuIds]").not("input:checked").size()>0) {  //如果有没有勾选的
   					$("#ckall").removeAttr("checked");
   				}
   				else{
   					$("#ckall").attr("checked","checked");
   				}
   			});
   			
   			//页面加载的时候,如果权限是全的,也应该将全选勾上
   			if($("input[name=menuIds]").not("input:checked").size()==0) {  //如果有没有勾选的
   				$("#ckall").attr("checked","checked");
   			}
   			
   		});
   	</script>
</body>
</html>	
